package org.pinda.network;

import java.io.IOException;
import java.net.SocketException;

import org.pinda.PindaConfig;
import org.pinda.gui.MultiplayerBrowserGui;
import org.pinda.network.browser.IBrowserPacket;
import org.pinda.network.browser.PacketRequestServers;

public class ServerBrowser extends Thread {
	
	private MultiplayerBrowserGui browserGui;
	/**
	 * The TCP Socket to the server browser
	 */
	private GameSocket browserSocket;
	
	public ServerBrowser(MultiplayerBrowserGui browserGui) {
		super("ServerBrowserSocket");
		this.browserGui = browserGui;
		browserSocket = new GameSocket(ConnectionType.SERVER_BROWSER);
	}
	
	@Override
	public void run() {
		try {
			browserSocket.connect(PindaConfig.getInstance().getString("server_browser"));
			browserSocket.sendPacket(new PacketRequestServers());
			while(!browserSocket.isClosed()) {
				IBrowserPacket packet = (IBrowserPacket)browserSocket.readPacket();
				packet.processClient(browserGui);
			}
		} catch (SocketException e) {
			System.err.println(e.getMessage());
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}

}
